package com.liming.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liming.domain.Book;
import com.liming.domain.User;
import com.liming.service.impl.BookServiceImpl;
import com.liming.utils.DBUtils;

public class CaigouBook extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		String num=request.getParameter("num");
		String adminname=(String)request.getSession().getAttribute("admin");
		BookServiceImpl service=new BookServiceImpl();
		
		Book book=service.findBook(id);
		String querysql="select * from Administrator where A_id=\'"+adminname+"\'";
		Connection conn=DBUtils.open();
		StringBuffer sb=new StringBuffer();
		Statement se;
		try {
			se = conn.createStatement();
			ResultSet rs=se.executeQuery(querysql);
			
			while(rs.next()){
				sb.append(rs.getString("S_id"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
		
			
			String sql1="insert into Putaway values( \'"+sb.toString()+"\',\'"+book.getbId()+"\',\'"+num+"\',"+(book.getbFixprice()*Integer.parseInt(num))+")";
			
			System.out.println(sql1);
			DBUtils.insert(sql1);
			String sql2="insert into Procurement values(\'"+adminname+"\',\'"+book.getbClass().substring(0,2)+"\',\'"+book.getbId()+"\',"+num+","+(book.getbFixprice()*Integer.parseInt(num))+",\'"+new Timestamp(new Date().getTime())+"\')";
			System.out.println(sql2);
			DBUtils.insert(sql2);
		response.sendRedirect(request.getContextPath()+"/servlet/GetCaigouBook");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);

	}

}
